{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "be0b542b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import json\n",
    "import matplotlib.pyplot as plt\n",
    "from PIL import Image, ImageDraw\n",
    "import numpy as np\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ac6b3b87",
   "metadata": {},
   "outputs": [],
   "source": [
    "def format_print_str(long_str, line_length=175):\n",
    "    formatted_str = '\\n'.join([long_str[i:i+line_length] for i in range(0, len(long_str), line_length)])\n",
    "    return formatted_str\n",
    "\n",
    "def calculate_iou(box1, box2):\n",
    "    x1_1, y1_1, x2_1, y2_1 = box1\n",
    "    x1_2, y1_2, x2_2, y2_2 = box2\n",
    "\n",
    "    x1_inter = max(x1_1, x1_2)\n",
    "    y1_inter = max(y1_1, y1_2)\n",
    "    x2_inter = min(x2_1, x2_2)\n",
    "    y2_inter = min(y2_1, y2_2)\n",
    "\n",
    "    if x2_inter > x1_inter and y2_inter > y1_inter:\n",
    "        area_inter = (x2_inter - x1_inter) * (y2_inter - y1_inter)\n",
    "    else:\n",
    "        area_inter = 0\n",
    "\n",
    "    area_box1 = (x2_1 - x1_1) * (y2_1 - y1_1)\n",
    "    area_box2 = (x2_2 - x1_2) * (y2_2 - y1_2)\n",
    "\n",
    "    area_union = area_box1 + area_box2 - area_inter\n",
    "\n",
    "    if area_union == 0:\n",
    "        return 0\n",
    "    iou = area_inter / area_union\n",
    "    return iou\n",
    "\n",
    "def print_content(content):\n",
    "    lines = content.splitlines()\n",
    "    for line in lines:\n",
    "        print(format_print_str(line))\n",
    "\n",
    "\n",
    "def show_thinking(ori_image, conversation, ori_gt_boxes):\n",
    "    for idx, conv in enumerate(conversation):\n",
    "        conv_role = conv['role']\n",
    "        content = conv['content']\n",
    "        if conv_role == 'system':\n",
    "            continue\n",
    "        elif idx == 1:\n",
    "            for _content in content:\n",
    "                if _content['type'] == 'text':\n",
    "                    _content = _content['text']\n",
    "                    _content = _content.split('\\n')[0]\n",
    "                    print (\"USER:   \")\n",
    "                    print_content(_content + '\\n\\n')\n",
    "        elif conv_role == 'assistant':\n",
    "            if isinstance(content, str):\n",
    "                _content = content\n",
    "            elif isinstance(content, list):\n",
    "                for _content in content:\n",
    "                    if _content['type'] == 'text':\n",
    "                        _content = _content['text']\n",
    "            else:\n",
    "                continue\n",
    "            print (\"ASSISTANT:   \")\n",
    "            print_content(_content)\n",
    "            if \"```json\" in _content:\n",
    "                _bbox_str = _content.split(\"```json\")[1].split(\"```\")[0]\n",
    "                _bbox = eval(_bbox_str)\n",
    "                for _box in _bbox:\n",
    "                    _box = _box['bbox_2d']\n",
    "                    x1, y1, x2, y2 = _box\n",
    "                    _crop_img = ori_image.crop((x1, y1, x2, y2))\n",
    "                    max_iou = 0.\n",
    "                    for _gt_box in ori_gt_boxes:\n",
    "                        _gt_x1, gt_y1, gt_w, gt_h = _gt_box\n",
    "                        _gt_x2, gt_y2 = _gt_x1 + gt_w, gt_y1 + gt_h\n",
    "                        _gt_box = (_gt_x1, gt_y1, _gt_x2, gt_y2)\n",
    "                        iou = calculate_iou(_box, _gt_box)\n",
    "                        if iou > max_iou:\n",
    "                            max_iou = iou\n",
    "                    print ('IOU: ', max_iou)\n",
    "                    print ('Area Size: ', _box[2] - _box[0], _box[3] - _box[1], (_box[2] - _box[0]) * (_box[3] - _box[1]), 4*28*28)\n",
    "\n",
    "                    plt.imshow(_crop_img)\n",
    "                    plt.axis('off')\n",
    "                    plt.show()\n",
    "            print_content('\\n\\n')\n",
    "        else:\n",
    "            if isinstance(content, str):\n",
    "                _content = content\n",
    "            elif isinstance(content, list):\n",
    "                for _content in content:\n",
    "                    if _content['type'] == 'text':\n",
    "                        _content = _content['text']\n",
    "            else:\n",
    "                continue\n",
    "            \n",
    "            print (\"USER:   \")\n",
    "            print_content(_content + '\\n\\n')\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "00aba36f",
   "metadata": {},
   "outputs": [],
   "source": [
    "root_path = 'PATH_OF_V*'\n",
    "json_path = 'PATH_OF_Result_JSONL'\n",
    "\n",
    "if 'direct_attributes' in json_path:\n",
    "    root_path = os.path.join(root_path, 'direct_attributes')\n",
    "else:\n",
    "    root_path = os.path.join(root_path, 'relative_position')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "89baa73c",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(json_path, 'r') as f:\n",
    "    lines = f.readlines()\n",
    "    lines = [json.loads(line) for line in lines]\n",
    "line_map = {}\n",
    "image_list = []\n",
    "for line in lines:\n",
    "    line_map[line['image']] = line\n",
    "    image_list.append(line['image'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f38b135f",
   "metadata": {},
   "outputs": [],
   "source": [
    "error_list = []\n",
    "for idx, image in enumerate(image_list):\n",
    "    acc = line_map[image]['acc']\n",
    "    if acc == 0:\n",
    "        error_list.append(idx)\n",
    "        \n",
    "print ('Error List: ', error_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1ed64ff9",
   "metadata": {},
   "outputs": [],
   "source": [
    "line_id = 6\n",
    "tosee_img = image_list[line_id]\n",
    "# tosee_img = \"sa_25219.jpg\"\n",
    "img_path = os.path.join(root_path, tosee_img)\n",
    "question = line_map[tosee_img]['question']\n",
    "answer = line_map[tosee_img]['answer']\n",
    "pred_ans = line_map[tosee_img]['pred_ans']\n",
    "pred_output = line_map[tosee_img]['pred_output']\n",
    "acc = line_map[tosee_img]['acc']\n",
    "print ('image: ', tosee_img)\n",
    "print (f\"question: {question}\")\n",
    "print (f\"answer: {answer}\")\n",
    "print (f\"pred_ans: {pred_ans}\")\n",
    "print (f\"acc: {acc}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b369782e",
   "metadata": {},
   "outputs": [],
   "source": [
    "ori_image_path = os.path.join(root_path, tosee_img)\n",
    "ori_json_path = os.path.join(root_path, tosee_img.replace('.jpg', '.json'))\n",
    "ori_json = json.load(open(ori_json_path, 'r'))\n",
    "ori_gt_name = ori_json['target_object']\n",
    "ori_gt_boxes = ori_json['bbox']\n",
    "\n",
    "box_color = (255, 0, 0) \n",
    "border_width = 2\n",
    "\n",
    "print ('image: ', tosee_img)\n",
    "print (f\"question: {question}\")\n",
    "print (f\"answer: {answer}\")\n",
    "print (f\"pred_ans: {pred_ans}\")\n",
    "print (f\"acc: {acc}\")\n",
    "\n",
    "ori_image = Image.open(ori_image_path)\n",
    "draw = ImageDraw.Draw(ori_image)\n",
    "crop_imgs = []\n",
    "for idx, _box in enumerate(ori_gt_boxes):\n",
    "    _box = [_box[0], _box[1], _box[2] + _box[0], _box[3] + _box[1]]\n",
    "    print (f\"{ori_gt_name[idx]}\")\n",
    "    print (_box)\n",
    "    x1, y1, x2, y2 = _box\n",
    "    _crop_img = ori_image.crop((x1, y1, x2, y2))\n",
    "    crop_imgs.append(_crop_img)\n",
    "\n",
    "plt.imshow(ori_image)\n",
    "plt.axis('off')\n",
    "plt.show()\n",
    "\n",
    "for i, _crop_img in enumerate(crop_imgs):\n",
    "    plt.subplot(1, len(crop_imgs), i + 1)\n",
    "    plt.imshow(_crop_img)\n",
    "    plt.axis('off')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "737dff00",
   "metadata": {},
   "outputs": [],
   "source": [
    "show_thinking(ori_image, pred_output, ori_gt_boxes)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "eval",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
